#include "Game.h"
#include "Renderer/D3D11Renderer.h"
#include "Game Objects/ObjectManager.h"

bool							Game::isRunning;
Timer							Game::timer;
int								Game::imageID = -1;
XMFLOAT2						Game::position;
float							Game::rotation;
HWND							Game::hwnd;
DirectInput*					Game::directInput;
int								Game::currMouseX;
int								Game::currMouseY;
int								Game::prevMouseX;
int								Game::prevMouseY;
DynamicObject*					Game::tarma;

bool Game::Initialize(HINSTANCE _hInstance, HWND _hWnd, bool _fullscreen, bool _bVsync, int _screenWidth, int _screenHeight)
{
	bool Success = true;

	position = XMFLOAT2(200.0f, 200.0f);
	hwnd = _hWnd;
	ShowCursor(true);

	directInput = new DirectInput;
	Success = directInput->Initialize(_hInstance, _hWnd, _screenWidth, _screenHeight);

	timer.Init();
		
	Success = D3D11Renderer::Initialize(_hWnd, true, true, _screenWidth, _screenHeight, false);

	Sprite::Initialize(_screenWidth, _screenHeight);

	tarma = new DynamicObject();

	tarma->Initialize("Tarma");

	tarma->SetPosition(XMFLOAT3(200, 200, 0.5f));
	tarma->SetVelocity(XMFLOAT3(0.0f, 0.0f, 0.0f));

	tarma->SetObjectID(ObjectManager::AddObject(tarma));

	LoadSpriteImages();

	isRunning = Success;

	return true;
}

void Game::Run()
{
	Update();
	Render();
}

void Game::Render()
{
	D3D11Renderer::ClearScene(XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f));

	RECT temp = {0, 0, 0, 0};
	//temp.bottom = 7;
	//temp.top = 47;
	//temp.right = 66;
	//temp.left = 34;

	Sprite::AddRenderCall(tarma->GetObjectID(), tarma->GetTexuresUsedFlag(), tarma->GetPosition().x, tarma->GetPosition().y, 1.0f, 1.0f, -rotation, temp, 0.4f);

	Sprite::AddRenderCall(tarma->GetObjectID(), tarma->GetTexuresUsedFlag(), Sprite::lightInformationCBuffer.lightPosition.x, Sprite::lightInformationCBuffer.lightPosition.y, .10f, .10f, -rotation, temp, 0.1f);

	//Sprite::AddRenderCall(imageID, position.x, position.y, 3.0f, 3.0f, -rotation, temp, 0.4f);

	//Sprite::AddRenderCall(imageID, 400.0f, 300.0f, 10.0f, 10.0f, -rotation, temp, 0.5f);

	Sprite::RenderFrame();

	D3D11Renderer::Present(D3D11Renderer::vsyncEnabled);
}

void Game::Update()
{
	timer.TimeStep();
	float deltaTime = (timer.GetDeltaTimeFloat() / 1000.0f);
	
	//rotation += (360 * deltaTime);
	
	//Get Input
	Input(deltaTime);
	tarma->Update(deltaTime);

	CalculateFrameStats();
}

void Game::Input(float _deltaTime)
{
	//Update the directInput
	directInput->Update();

	directInput->GetMouseLocation(currMouseX, currMouseY);

	XMFLOAT3 velocity = XMFLOAT3(0.0f, 0.0f, 0.0f);

	if(directInput->IsKeyPressed(DIK_W))
	{
		velocity.y = 100.0f;
	}
	if(directInput->IsKeyPressed(DIK_S))
	{
		velocity.y = -100.0f;
	}
	if(directInput->IsKeyPressed(DIK_A))
	{
		velocity.x = -100.0f;
	}
	if(directInput->IsKeyPressed(DIK_D))
	{
		velocity.x = 100.0f;
	}

	if(directInput->IsKeyPressed(DIK_LEFTARROW))
	{
		Sprite::lightInformationCBuffer.lightPosition.x -= (100.0f * _deltaTime);
	}
	if(directInput->IsKeyPressed(DIK_RIGHTARROW))
	{
		Sprite::lightInformationCBuffer.lightPosition.x += (100.0f * _deltaTime);
	}
	if(directInput->IsKeyPressed(DIK_UPARROW))
	{
		Sprite::lightInformationCBuffer.lightPosition.y += (100.0f * _deltaTime);
	}
	if(directInput->IsKeyPressed(DIK_DOWNARROW))
	{
		Sprite::lightInformationCBuffer.lightPosition.y -= (100.0f * _deltaTime);
	}
	if(directInput->IsKeyPressed(DIK_LSHIFT))
	{
		Sprite::lightInformationCBuffer.lightPosition.z += (100.0f * _deltaTime);
	}
	if(directInput->IsKeyPressed(DIK_RSHIFT))
	{
		Sprite::lightInformationCBuffer.lightPosition.z -= (100.0f * _deltaTime);
	}

	if(directInput->IsEscapePressed())
	{
		isRunning = false;
	}
	tarma->SetVelocity(velocity);
	prevMouseX = currMouseX;
	prevMouseY = currMouseY;
}

void Game::Exit()
{

	int* something = new int[1];
	D3D11Renderer::Shutdown();

	if(directInput)
	{
		directInput->Shutdown();
		delete directInput;
		directInput = NULL;
	}
}

void Game::CalculateFrameStats()
{
	static int frameCount = 0;
	static float elapsedTime = 0.0f;

	elapsedTime += (timer.GetDeltaTimeFloat() / 1000.0f);

	frameCount ++;

	if(elapsedTime >= 1.0f)
	{
		float framesPerSec = frameCount / elapsedTime;

		string FPS;
		char MyInfo[16];
		sprintf_s(MyInfo, "%f", framesPerSec);
		FPS += MyInfo;
		SetWindowText(D3D11Renderer::hwnd, FPS.c_str());

		elapsedTime = 0.0f;
		frameCount = 0;
	}
}

void Game::LoadSpriteImages()
{
	tarma->AddTexture(DIFFUSE_TEXTURE, L"Kro_Diffuse.dds");
	tarma->AddTexture(NORMAL_TEXTURE, L"Kro_Normal.dds");
	//tarma.AddTexture(NORMAL_TEXTURE, L"TarmaRoll.dds");
	//imageID = Sprite::AddImage(L"TarmaRoll.dds");
}